<template>
  <div class="article-manage">
    <!-- 搜索筛选区 -->
    <div class="filter-container">
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="文章标题">
          <el-input v-model="queryParams.title" placeholder="请输入文章标题"></el-input>
        </el-form-item>
        <el-form-item label="文章分类">
          <el-select v-model="queryParams.category" placeholder="请选择文章分类">
            <el-option label="前端" value="frontend"></el-option>
            <el-option label="后端" value="backend"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章标签">
          <el-select v-model="queryParams.tags" multiple placeholder="请选择文章标签">
            <el-option label="Vue3" value="vue3"></el-option>
            <el-option label="TypeScript" value="typescript"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
            v-model="queryParams.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮区 -->
    <div class="operation-container">
      <el-button type="primary" @click="handleAdd">新增文章</el-button>
    </div>

    <!-- 文章列表 -->
    <el-table :data="articleList" style="width: 100%">
      <el-table-column prop="id" label="序号" width="80"></el-table-column>
      <el-table-column prop="title" label="文章标题"></el-table-column>
      <el-table-column prop="description" label="文章描述"></el-table-column>
      <el-table-column prop="category" label="分类"></el-table-column>
      <el-table-column prop="tags" label="标签">
        <template slot-scope="scope">
          <el-tag v-for="tag in scope.row.tags" :key="tag" style="margin-right: 5px">
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isPublic" label="是否置顶">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isPublic"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="success" @click="handlePreview(scope.row)">预览</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ArticleManage',
  data() {
    return {
      queryParams: {
        title: '',
        category: '',
        tags: [],
        dateRange: [],
        page: 1,
        pageSize: 10
      },
      articleList: [],
      total: 0
    }
  },
  created() {
    this.getArticleList()
  },
  methods: {
    // 获取文章列表
    async getArticleList() {
      // TODO: 调用API获取文章列表
      this.articleList = [
        {
          id: 1,
          title: '半年薄码，归来已',
          description: '重见天日，世界已...',
          category: '前端',
          tags: ['vue3', 'typescript'],
          isPublic: true
        }
      ]
      this.total = this.articleList.length
    },
    // 搜索
    handleSearch() {
      this.queryParams.page = 1
      this.getArticleList()
    },
    // 重置
    handleReset() {
      this.queryParams = {
        title: '',
        category: '',
        tags: [],
        dateRange: [],
        page: 1,
        pageSize: 10
      }
      this.getArticleList()
    },
    // 新增文章
    handleAdd() {
      this.$router.push('/edit')
    },
    // 编辑文章
    handleEdit(row) {
      this.$router.push(`/edit?id=${row.id}`)
    },
    // 预览文章
    handlePreview() {
      // TODO: 实现预览功能
    },
    // 删除文章
    handleDelete() {
      this.$confirm('确认删除该文章吗？', '提示', {
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除API
        this.$message.success('删除成功')
        this.getArticleList()
      }).catch(() => {})
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getArticleList()
    },
    // 页码改变
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getArticleList()
    }
  }
}
</script>

<style scoped>
.article-manage {
  padding: 20px;
}
.filter-container {
  margin-bottom: 20px;
}
.operation-container {
  margin-bottom: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 